from lly.vim.command import cmd_result_path, execute_os_cmd, cmd_history
show_info_window_name = "cmd_history"


class OsInfo():
    def __init__(self, nvim):
        self.cmd_history_path = str(cmd_result_path(
            "cmd_history", suffix='.cmd_history'))
        self.cmd_result_path = str(cmd_result_path(
            "cmd_result", suffix=".cmd_result"))
        self.cmd_history_window = None
        self.cmd_result_window = None
        self.cmd_history_buffer = None
        self.cmd_result_buffer = None
        self.vim = nvim

    def history_window(self):
        if self.cmd_history_window == None \
                or self.cmd_history_window not in self.vim.windows \
                or self.cmd_history_window.buffer != self.cmd_history_buffer:
            self.cmd_history_window = self.find_window(self.cmd_history_path)
            self.cmd_history_buffer = self.cmd_history_window.buffer
        return self.cmd_history_window

    def result_window(self):
        if self.cmd_result_window == None \
                or self.cmd_result_window not in self.vim.windows \
                or self.cmd_result_window.buffer != self.cmd_result_buffer:
            self.cmd_result_window = self.find_window(self.cmd_result_path)
            self.cmd_result_buffer = self.cmd_result_window.buffer
        return self.cmd_result_window

    def find_window(self, buffer_path):
        self.vim.command(f'new {buffer_path}')
        for window in self.vim.windows:
            if window.buffer.name == buffer_path:
                info_window = window
                break
        return info_window


def execute_cmd(cmd):
    cmd_info = execute_os_cmd(cmd)
    result = cmd
    if cmd_info['returncode'] == 0:
        result += "-- execute ok"
    else:
        result += "-- execute failure"
    print(result)


